package sust.cse.ruhul.jobs1;

import java.io.File;
import java.util.Date;

import sust.cse.ruhul.jpbs1.util.CameraUtil;
import sust.cse.ruhul.jpbs1.util.FileUtil;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Page4 extends Activity {
	// private static final int CAMERA_REQUEST = 1888;
	private static final int REQUEST_TAKE_PICTURE = 1;
	// Model
//	private Uri cameraFileUri;
	// private Bitmap photo = null;
	// public static Bitmap p = null;
	// View
	private RelativeLayout panel7;
	private Button btnSelectedItemLabel;
	private String val = null;
	private ImageView captureImage, searchIcon;
	private CheckBox jobCompletedCheck;
	private TextView whenKey, whenValue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.page4);
		jobCompletedCheck = (CheckBox) findViewById(R.id.job_completed_check);
		whenKey = (TextView) findViewById(R.id.when_key);
		whenValue = (TextView) findViewById(R.id.when_value);
		btnSelectedItemLabel = (Button) findViewById(R.id.btn_selected_item_label);
		panel7 = (RelativeLayout) findViewById(R.id.panel7);
		panel7.setVisibility(View.INVISIBLE);
		captureImage = (ImageView) findViewById(R.id.capture_image);
		searchIcon = (ImageView) findViewById(R.id.search_icon);
		// final Intent intent = getIntent();
		// val = intent.getStringExtra("item");
		// btnSelectedItemLabel.setText(val);

	}

	private void takePicture() {
		if (!CameraUtil.hasCamera())
			return;
		File f = FileUtil.createEmptyCameraFile();
		
		FileUtil.cameraFileUri = Uri.fromFile(f);
		
		Log.e("Path ",""+FileUtil.cameraFileUri.getPath());
		
		final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, FileUtil.cameraFileUri);
		startActivityForResult(intent, REQUEST_TAKE_PICTURE);
	}

	public void onButtonClickOfPage4(final View v) {
		switch (v.getId()) {
		case R.id.btn_capture:
			takePicture();
			// Intent cameraIntent = new
			// Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			// startActivityForResult(cameraIntent, CAMERA_REQUEST);

			// Toast.makeText(Page4.this, "WOrking...",
			// Toast.LENGTH_SHORT).show();
			break;

		case R.id.btn_send_do_for_review:
			panel7.setVisibility(View.VISIBLE);
			// Toast.makeText(Page4.this, "WOrking...",
			// Toast.LENGTH_SHORT).show();
			break;
		case R.id.capture_image:
		case R.id.search_icon:
			// final LinearLayout.LayoutParams p = new
			// LinearLayout.LayoutParams(
			// LinearLayout.LayoutParams.MATCH_PARENT,
			// LinearLayout.LayoutParams.MATCH_PARENT, 1);
			// captureImage.setLayoutParams(p);

			final Intent intent = new Intent(Page4.this, ImagePreview.class);
			intent.setData(FileUtil.cameraFileUri);
			startActivity(intent);
			break;
		case R.id.job_completed_check:
			if (jobCompletedCheck.isChecked()) {
				whenValue.setText(new Date().toString());
				whenKey.setVisibility(View.VISIBLE);
				whenValue.setVisibility(View.VISIBLE);
			} else {
				whenKey.setVisibility(View.VISIBLE);
				whenValue.setVisibility(View.INVISIBLE);
			}
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// if (requestCode == CAMERA_REQUEST)
		// {
		// photo = (Bitmap) data.getExtras().get("data");
		// p = photo;
		// captureImage.setImageBitmap(photo);
		// captureImage.setVisibility(View.VISIBLE);
		// searchIcon.setVisibility(View.VISIBLE);
		//
		// }
		System.out.println("------------------ onActi " + FileUtil.cameraFileUri.getPath());
		if (requestCode == REQUEST_TAKE_PICTURE
				&& resultCode == Activity.RESULT_OK) {
			// captureImage.setImageURI(cameraFileUri);
			captureImage.setVisibility(View.VISIBLE);
			searchIcon.setVisibility(View.VISIBLE);
			// System.out.println("------------------ onActi okkk");
			captureImage.setImageBitmap(decodeSampledBitmapFromResource(
					FileUtil.cameraFileUri.getPath(), 100, 100));

		}
	}

	public static Bitmap decodeSampledBitmapFromResource(final String path,
			int reqWidth, int reqHeight) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		Bitmap bm = BitmapFactory.decodeFile(path, options);
		Bitmap rotatedBitmap = bm;
		// try {
		// ExifInterface exif = new ExifInterface(path);
		// String orientString = exif
		// .getAttribute(ExifInterface.TAG_ORIENTATION);
		// int orientation = orientString != null ? Integer
		// .parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
		// int rotationAngle = 0;
		// if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
		// rotationAngle = 90;
		// if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
		// rotationAngle = 180;
		// if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
		// rotationAngle = 270;
		//
		// Matrix matrix = new Matrix();
		// matrix.setRotate(rotationAngle, (float) reqWidth / 2,
		// (float) reqHeight / 2);
		// rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, options.outWidth,
		// options.outHeight, matrix, true);
		//
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		return bm;
	}

	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {

			final int halfHeight = height / 2;
			final int halfWidth = width / 2;

			// Calculate the largest inSampleSize value that is a power of 2 and
			// keeps both
			// height and width larger than the requested height and width.
			while ((halfHeight / inSampleSize) > reqHeight
					&& (halfWidth / inSampleSize) > reqWidth) {
				inSampleSize *= 2;
			}
		}

		return inSampleSize;
	}

}